import {
	HTTP
} from '../utils/http.js';

import util from '../utils/util.js';

export class homeModule extends HTTP {
	getHomeData() { // 获取首页参数
		const userData = util.getStorage('userData')
		console.log(userData.userID)
		return this.request({
			url: '/api/HomePage/GetHomePage',
			data: {
				userID: userData.userID
			}
		})
	}

	getCalendarData(year, month, day) {
		return new Promise((resolve, reject) => {
			uni.request({
				url: 'http://v.juhe.cn/laohuangli/d',
				method: 'GET',
				data: {
					key: '968e9e87d76fba3653b87420d99f6396',
					date: year + '-' + month + '-' + day
				},
				success: res => {
					// console.log(res)
					let data = null
					if(res.data.result){
						data = this._screeningCalendar(res.data.result)
					}else {
						data = {
							yinli: '暂时无法获取',
							yi: '请见谅'
						}
					}
					
					resolve(data)
				},
				fail: err => {
					reject(err)
				}
			})
		})
	}
	
	getLinkMan(empName='') {
		return this.request({
				url: '/api/Users/GetLinkMan',
				data: {
					empName
				}
		})
	}
	
	getTasType(neiID) {
		return this.request({
			url: '/api/Users/GetTasType',
			data:{
				neiID
			}
		})
	}
	
	getTaskManual(tasType) {
		return this.request({
			url: '/api/Users/GetTaskManual',
			data:{
				tasType
			}
		})
	}

	_screeningCalendar(data) { // 截取宜字符串的长度，取前三个词
		var shiYi = data.yi.split(' ')
		var yi = shiYi.slice(0, 3)
		return {
			yinli: data.yinli,
			yi: yi.join(" ")
		}
	}

	drawCanvas(id, maxNum, windowWidth) { // 构建任务完成进度方法
	
		const rate = 17.8571429
		let WideCircle = parseInt(windowWidth/rate)

		const context = uni.createCanvasContext(id) // 构建上层展示画布对象
		const context_1 = uni.createCanvasContext(id + '1') // 构建底部背景画布对象

		let index = 0; //定义一个画布圆弧线的初始值
		let num = 0; //定义进度数字的初始值
		this._drawCircular(context_1, "#eee", 2, WideCircle) // 绘制底层画布
		
		if (maxNum == 0) { // 当进度等于0 则不执行下面脚本
			return
		}

		let timing = setInterval(()=> { //使用计时器对 index、num做增值
			if (num == maxNum) {
				clearInterval(timing) // 当num 等于设置的值时 结束 计时器
			}
			index += 0.02 //控制圆弧线增长频率
			num += 1 //控制进度数字 增长
			this._drawCircular(context, "#2A89F8", index , WideCircle)
		}, 15) //15毫秒为计时器的间隔时间 
		
	}
	
	_drawCircular(context, color, index, WideCircle) { // 绘制画布圆形的私有方法
		context.setStrokeStyle(color) //弧线的颜色
		context.setLineWidth(13) //弧线的宽度
		context.arc(30, 30, WideCircle , 0, index * Math.PI, false) //圆在画布坐标点，及 圆方向，false为顺时针
		context.stroke()
		context.draw()
	}
}





